#include "PreCompiled.h"
#include "WorkbenchManager.h"
#include "Workbench.h"
WorkbenchManager*WorkbenchManager::_instance = 0;

WorkbenchManager::WorkbenchManager(void)
{
}

WorkbenchManager::~WorkbenchManager(void)
{
}
WorkbenchManager* WorkbenchManager::instance()
{
	if(_instance == 0)
		_instance = new WorkbenchManager;
	return _instance;
}
Workbench* WorkbenchManager::createWorkbench(const std::string& name)
{
    Workbench* wb = getWorkbench(name);
	if(!wb)
	{
		wb = new Workbench;
		wb->setName(name);
		_workbenches[name] = wb;
	}
	return wb;
}
void WorkbenchManager::removeWorkbench(const std::string& name)
{
	std::map<std::string,Workbench*>::iterator it = _workbenches.find(name);
	if(it != _workbenches.end())
	{
		Workbench* wb = it->second;
		_workbenches.erase(it);
		if(_activeWorkbench == wb)
			_activeWorkbench = 0;
		delete wb;
		wb = NULL;

	}
}

Workbench* WorkbenchManager::getWorkbench(const std::string& name)const
{
	Workbench* wb = 0;
	std::map<std::string,Workbench*>::const_iterator it = _workbenches.find(name);
	if(it != _workbenches.end())
	{
		wb =  it->second;
	}
	return wb;
}

bool WorkbenchManager::activate(const std::string& name)
{
   Workbench* wb = createWorkbench(name);
   if(wb)
   {
	   _activeWorkbench = wb;
	   wb->activate();
	   return true;
   }
   return false;
}

Workbench* WorkbenchManager::active()const
{
    return _activeWorkbench;
}

std::list<std::string> WorkbenchManager::workbenches()
{
	std::list<std::string> wb;
	std::map<std::string,Workbench*>::const_iterator it = _workbenches.begin();
	for(; it != _workbenches.end(); it++)
	{
         wb.push_back(it->first);
	}

     return wb;
}





